﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Model;
using University.Workshop.Business;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class NewDiagnosticPatient : Form
    {
        Diagnostic diagnostic = new Diagnostic();
        private OpenFileDialog openFile = new OpenFileDialog();
        public NewDiagnosticPatient(Patient patient)
        {
            InitializeComponent();
            LoadDiagnosticPatient(patient);
            ObtainNQuery();
            //IssuePermits();
        }

        //private void IssuePermits()
        //{
        //    //buttonSave.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //}

        private void LoadDiagnosticPatient(Patient patient)
        {
            int age = patient.Birthday.Year;
            string fullNameDoctor = Main.userMenu[0] + "," + Main.userMenu[1];
            labelCiPatient.Text = patient.CIPatient;
            labelFullNamePatient.Text = patient.LastName + ", " + patient.FirstName;
            labelGenderPatient.Text = patient.Gender;
            labelAgePatient.Text = Convert.ToString(DateTime.Today.Year-age);            
            labelDate.Text = DateTime.Today.ToShortDateString();
            labelNameDoctor.Text = fullNameDoctor;
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", patient.ImagePatient)));
                pictureBoxPatient.Image = picture;
            }
            catch (Exception)
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", "anonimos.jpg")));
                pictureBoxPatient.Image = picture;
            } 
        }
        private void ObtainNQuery() 
        {
            int add=Convert.ToInt32(DiagnosticManager.ObtainNunQuery());
            labelNroQuery.Text = Convert.ToString(add + 1);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            LoadNewDiagnosticPatient();
            if (!string.IsNullOrEmpty(diagnostic.NameDiagnostics) && !string.IsNullOrEmpty(diagnostic.NameTreatments))
            {
                if (DiagnosticManager.InsertDiagnosticPatient(diagnostic))
                {
                    MessageBox.Show("Successful registration");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("The Diagnostic Patient could not be registered");
                }
            }
            else
            {
                MessageBox.Show("You must select at least one diagnosis and treatment");
            }
        }

        private void LoadNewDiagnosticPatient()
        {
            string ciDoctor = Main.userMenu[3];
            diagnostic.CIPatient = int.Parse(labelCiPatient.Text);
            diagnostic.CIDoctor = int.Parse(ciDoctor);
            diagnostic.DateDiagnostic = Convert.ToDateTime(labelDate.Text);
            string stringDiagnostic = (checkBoxPeriodontalDisease.Checked)?checkBoxPeriodontalDisease.Text + "-":null;
            stringDiagnostic += (checkBoxCysticPathology.Checked) ? checkBoxCysticPathology.Text + "-" : null;
            stringDiagnostic += (checkBoxDestructiveDentalPathology.Checked) ? checkBoxDestructiveDentalPathology.Text + "-" : null;
            stringDiagnostic += (checkBoxPathologyATM.Checked) ? checkBoxPathologyATM.Text + "-" : null;
            stringDiagnostic += (checkBoxEdentulism.Checked) ? checkBoxEdentulism.Text + "-" : null;
            stringDiagnostic += (checkBoxImpactedTeeth.Checked) ? checkBoxImpactedTeeth.Text + "-" : null;            
            stringDiagnostic += textBoxOthersDiagnostic.Text;
            diagnostic.NameDiagnostics = stringDiagnostic;

            string treatments = (checkBoxExtractions.Checked) ? checkBoxExtractions.Text + "-" : null;
            treatments += (checkBoxTreatmentOfPeriodontalDiseaseThroughBasicPhase.Checked) ? checkBoxTreatmentOfPeriodontalDiseaseThroughBasicPhase.Text + "-" : null;
            treatments += (checkBoxTreatmentOfPeriodontalDiseaseBySurgeryInSextant.Checked) ? checkBoxTreatmentOfPeriodontalDiseaseBySurgeryInSextant.Text + "-" : null;
            treatments += (checkBoxRootCanalTreatment.Checked) ? checkBoxRootCanalTreatment.Text + "-" : null;
            treatments += (checkBoxPeriapicalSurgery.Checked) ? checkBoxPeriapicalSurgery.Text + "-" : null;
            treatments += (checkBoxOralSurgery.Checked) ? checkBoxOralSurgery.Text + "-" : null;
            treatments += (checkBoxFillings.Checked) ? checkBoxFillings.Text + "-" : null;
            treatments += (checkBoxInlays.Checked) ? checkBoxInlays.Text + "-" : null;
            treatments += (checkBoxVeneers.Checked) ? checkBoxVeneers.Text + "-" : null;
            treatments += (checkBoxOrthodontics.Checked) ? checkBoxOrthodontics.Text + "-" : null;
            treatments += (checkBoxPlacementOfDentalImplants.Checked) ? checkBoxPlacementOfDentalImplants.Text + "-" : null;
            treatments += (checkBoxProstheticRehabilitation.Checked) ? checkBoxProstheticRehabilitation.Text : null;
            diagnostic.NameTreatments = treatments;

            string unfavorable = (checkBoxPoorHygiene.Checked) ? checkBoxPoorHygiene.Text + "-" : null;
            unfavorable += (checkBoxSmoker.Checked) ? checkBoxSmoker.Text + "-" : null;
            unfavorable += (checkBoxDryMouth.Checked) ? checkBoxDryMouth.Text + "-" : null;
            unfavorable += (checkBoxDiabetesMellitus.Checked) ? checkBoxDiabetesMellitus.Text + "-" : null;
            unfavorable += (checkBoxGummySmile.Checked) ? checkBoxGummySmile.Text + "-" : null;
            unfavorable += (checkBoxRespiratoryDistress.Checked) ? checkBoxRespiratoryDistress.Text + "-" : null;
            unfavorable += (checkBoxExtractionOfImpactedTeeth.Checked) ? checkBoxExtractionOfImpactedTeeth.Text + "-" : null;
            unfavorable += (checkBoxUpperPosteriorImplants.Checked) ? checkBoxUpperPosteriorImplants.Text + "-" : null;
            unfavorable += (checkBoxSubsequentLowerImplants.Checked) ? checkBoxSubsequentLowerImplants.Text + "-" : null;
            unfavorable += textBoxOthersUnfavorable.Text;
            diagnostic.AdverseCircumstancesPatient = unfavorable;

            diagnostic.Observation = textBoxObservationPatient.Text;
            diagnostic.StateDiagnostic = "Activo";
        }

        private void checkBoxOthers_MouseClick(object sender, MouseEventArgs e)
        {
            if (checkBoxOthers.Checked)
            {
                textBoxOthersDiagnostic.ReadOnly = false;
            }
            else 
            {
                textBoxOthersDiagnostic.Text = null;
                textBoxOthersDiagnostic.ReadOnly = true;
                errorProviderDiagnosticPatient.SetError(textBoxOthersDiagnostic, null);
            }
        }

        private void checkBoxOthersUnfavorable_MouseClick(object sender, MouseEventArgs e)
        {
            if (checkBoxOthersUnfavorable.Checked)
            {
                textBoxOthersUnfavorable.ReadOnly = false;
            }
            else
            {
                textBoxOthersUnfavorable.Text = null;
                textBoxOthersUnfavorable.ReadOnly = true;
                errorProviderDiagnosticPatient.SetError(textBoxOthersUnfavorable, null);                
            }
        }

        private void textBoxOthersDiagnostic_Validating(object sender, CancelEventArgs e)
        {
            errorProviderDiagnosticPatient.SetError(textBoxOthersDiagnostic, ValidateFields.NameValidation(textBoxOthersDiagnostic.Text));
        }
        private void textBoxOthersUnfavorable_Validating(object sender, CancelEventArgs e)
        {
            errorProviderDiagnosticPatient.SetError(textBoxOthersUnfavorable,ValidateFields.NameValidation(textBoxOthersUnfavorable.Text));
        }        
    }
}
